package cn.hs.lyxf.utils;

import cn.hutool.core.codec.Base64;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @Package: cn.mzy.xfcc.utils
 * @ClassName: TwoDimensionalCode
 * @Date:2022/11/28 16:51
 * @Description:
 */
public class TwoDimensionalCode {
    //text 扫描二维码展示的内容,width宽度，height长度
    public static String generateQRCodeImage(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        //Path path = FileSystems.getDefault().getPath("3.png");
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        //MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        return Base64.encode(pngData);
    }
}
